<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Patient extends Controller_DefaultTemplate {

    public function action_index() {
        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/index");
    }

    public function action_ctscan(){
        $p_id = "";
        $c_id = "";

        if(isset($_GET['method'])){
            if($_GET['method']=='del'){
                $ct_id = $_GET['ct_id'];
                $sql = "SELECT * FROM ct_scan where ct_id=".$ct_id;
                $ct = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
                if(count($ct)!=0){
                    $filename = "ct/".$ct[0]['p_id']."/".$ct[0]['year']."/".$ct[0]['month']."/".$ct[0]['filename'];
                    $real = realpath($filename);
                    unlink($real);
                    DB::delete("ct_scan")
                    ->where('ct_id', '=',$ct_id)
                    ->execute();
                }
            }
        }

        if(isset($_GET['p_id'])){
            $p_id = $_GET['p_id'];
            $c_id = $_GET['c_id'];
        }
        
        $sql = "SELECT * FROM ct_scan where p_id=".$p_id." and c_id=".$c_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();

        if(count($query)!=0){
            $ct_images = $query;
        }else{
            $ct_images = null;
        }
        
        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/smoothness/jquery-ui-1.8.7.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/ctscan")
                                    ->set('c_id',$c_id)
                                    ->set('ct_images',$ct_images)
                                    ->set('p_id',$p_id);
    }

    public function action_history() {
        $consult = new Model_Consult();
        $std = null;
        $nihss = null;
        $stp = null;
        $est = null;
        $lab = null;

        if(isset($_GET['p_id'])) {
            $p_id = $_GET['p_id'];
            $c_id = $_GET['c_id'];

            //Load current data
            $std = $consult->loadStrokeData($p_id, $c_id);
            $nihss = $consult->loadNIHSSData($p_id, $c_id);
            $lab = $consult->loadLaboratoryData($p_id, $c_id);
            $stp = $consult->loadStrokePhy($p_id, $c_id);
            $est = $consult->loadEstimateData($p_id, $c_id);
            $history = $consult->getConsultHistoryByCID($p_id, $c_id);
            $patient_model = new Model_Patient();
            $patient = $patient_model->getPatientProfileFromPID($p_id);

            $this->template->title            = 'TeleStroke';
            $this->template->meta_keywords    = 'TeleStroke';
            $this->template->meta_description = 'TeleStroke';
            $this->template->styles  = array('assets/css/smoothness/jquery-ui-1.8.7.custom.css' => 'screen');
            $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
            $history_view = View::factory("patient/history")
                    ->set('patient',$patient[0])
                    ->set('std',$std)
                    ->set('stp',$stp)
                    ->set('p_id',$p_id)
                    ->set('c_id', $c_id)
                    ->set('lab',$lab)
                    ->set('est',$est)
                    ->set('history',$history)
                    ->set('nihss', $nihss);

            $this->template->content = $history_view;
        }
    }

    public function action_profile($p_id) {
        $histories = null;
        $this->template->title            = 'TeleStroke';
        $patient_model = new Model_Patient();
        $patient= $patient_model->getPatientProfileFromPID($p_id);

        if(count($patient[0])!=0) {
            $this->template->title            = 'TeleStroke';
            $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
            $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');

            //Get consult_history
            $consult = new Model_Consult();
            $histories = $consult->getConsultHistory($p_id);
            
            $this->template->content = View::factory("patient/profile")
                    ->set('patient', $patient[0])
                    ->set('histories',$histories);
        }else {
            echo "No patient";
        }
    }

    public function action_cancelContract(){
        $consult = new Model_Consult();
        $p_id = $_GET["p_id"];
        $c_id = $consult->getConsultID($p_id);

        DB::update('consult_status')
                ->set(array(
                'consult_doctor_id'=>'0',
                'status'=>"select_doctor"
                ))
                ->where('c_id','=', $c_id)
                ->and_where('p_id', '=',$p_id)
                ->execute();

        
        $doctors = $consult->getAllConsultDoctor();
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/choosedoctor")
                ->set('p_id',$p_id)
                ->set('c_id',$c_id)
                ->set('doctors',$doctors);
    }

    public function action_contract() {
        $patient_model = new Model_Patient();
        $consult = new Model_Consult();
        if(isset($_POST['p_id'])) {
            $p_id = $_POST['p_id'];
            $c_id = $consult->getConsultID($p_id);
        }elseif(isset($_GET['p_id'])) {
            $p_id = $_GET['p_id'];
            $c_id = $consult->getConsultID($p_id);
        }else {
            $this->request->redirect("../patient/");
        }

        if($consult->checkStatus($p_id, $c_id)=="Waiting to consult"){
             $consult_doctor_id = $consult->getConsultDoctorID($p_id, $c_id);
             $this->request->redirect("../patient/waiting?consult_doctor_id=".$consult_doctor_id."&p_id=".$p_id."&c_id=".$c_id);
        }else if($consult->checkStatus($p_id, $c_id)=="Consulting"){
             $this->request->redirect("../patient/livecamera?p_id=".$p_id."&c_id=".$c_id);
        }
        
        $places = $patient_model->loadPatientPlace();
        $doctor_name = "";
        $sql = "SELECT * FROM consult_status where p_id=".$p_id;
        $query = Database::instance()->query(Database::SELECT, $sql, FALSE)->as_array();
        if(count($query)!=0) {
           $doctor_name = $query[0]['doctor_name'];
        }

        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/precontract")
                ->set('p_id',$p_id)
                ->set('doctor_name',$doctor_name)
                ->set('places',$places);
    }

    public function action_precontract_save() {
        $consult = new Model_Consult();
        $p_id = $_POST["p_id"];
        $c_id = $consult->getConsultID($p_id);
        $consult->updatePrecontract($p_id, $c_id, $_POST);

        $doctors = $consult->getAllConsultDoctor();
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/choosedoctor")
                ->set('p_id',$p_id)
                ->set('c_id',$c_id)
                ->set('doctors',$doctors);

    }

    public function action_waiting() {
        $consult_doctor_id = $_GET['consult_doctor_id'];
        $c_id = $_GET['c_id'];
        $p_id = $_GET['p_id'];
        $consult = new Model_Consult();
        $consult->updateToWaitingToConsultStatus($p_id, $c_id,$consult_doctor_id);

        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/jquery.timer.js');
        $this->template->content = View::factory("patient/waiting")
                ->set('c_id',$c_id)
                ->set('p_id',$p_id)
                ->set('consult_doctor_id',$consult_doctor_id);

    }

    public function action_nihss() {
        $this->template->title            = 'NIHSS';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/smoothness/jquery-ui-1.8.7.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/nihss");
    }

    public function action_consultpage() {
        if(isset($_POST['p_id'])) {
            $p_id = $_POST['p_id'];
            $patient_model = new Model_Patient();
            $patient = $patient_model->getPatientProfileFromPID($p_id);
        }
        else if(isset($_GET['p_id'])) {
            $p_id = $_GET['p_id'];
            $patient_model = new Model_Patient();
            $patient = $patient_model->getPatientProfileFromPID($p_id);
        }
        else {
            $this->request->redirect("../patient/");
        }
        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/smoothness/jquery-ui-1.8.7.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');
        $this->template->content = View::factory("patient/consultpage")                
                ->set('p_id',$p_id);
    }

    public function action_livecamera() {
        $c_id = $_GET['c_id'];
        $p_id = $_GET['p_id'];
        $consult = new Model_Consult();
        $ipcamera = $consult->getIPCameraData($p_id, $c_id);
        $doctor = $consult->getDoctorData($p_id,$c_id);
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/jquery.timer.js');
        $this->template->content = View::factory("camera/patient")
                ->set('url',$ipcamera['pp_url'])
                ->set('username',$ipcamera['pp_username'])
                ->set('password',$ipcamera['pp_password'])
                ->set('doctor',$doctor)
                ->set('p_id',$p_id)
                ->set('c_id',$c_id)
            ;
    }

    public function action_consult() {
        $consult = new Model_Consult();
        $patient_model = new Model_Patient();
        $std = null;
        $nihss = null;
        $stp = null;
        $est = null;
        $lab = null;
        $mng = null;
        $patient = null;

        if(isset($_POST['p_id'])) {
            $p_id = $_POST['p_id'];
            $c_id = $consult->getConsultID($p_id);
            
            $patient = $patient_model->getPatientProfileFromPID($p_id);
        }elseif(isset($_GET['p_id'])) {
            $p_id = $_GET['p_id'];
            $c_id = $consult->getConsultID($p_id);
            $patient = $patient_model->getPatientProfileFromPID($p_id);
        }else {
            $this->request->redirect("../patient/");
        }

        if($consult->isConsulting($c_id)) {
            //Load current data
            $std = $consult->loadStrokeData($p_id, $c_id);
            $nihss = $consult->loadNIHSSData($p_id, $c_id);
            $lab = $consult->loadLaboratoryData($p_id, $c_id);
            $stp = $consult->loadStrokePhy($p_id, $c_id);
            $est = $consult->loadEstimateData($p_id, $c_id);
            $mng = $consult->loadManagementData($p_id, $c_id);
        }else {
            $consult->updateToStartStatus($p_id,$c_id);
        }

        $this->template->title            = 'TeleStroke';
        $this->template->meta_keywords    = 'TeleStroke';
        $this->template->meta_description = 'TeleStroke';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min','assets/js/consult.js','assets/js/jquery.timer.js');
        $consult_view = View::factory("patient/consult")
                ->set('std',$std)
                ->set('stp',$stp)
                ->set('p_id',$p_id)
                ->set('c_id', $c_id)
                ->set('lab',$lab)
                ->set('est',$est)
                ->set('mng',$mng)
                ->set('patient',$patient[0])
                ->set('nihss', $nihss);
        $this->template->content = $consult_view;
    }

    public function action_search() {
        $keyword = $_POST["patient_key"];
        $patient_model = new Model_Patient();

        $this->template->title            = 'Patient Search Result';
        $this->template->styles  = array('assets/css/start/jquery-ui-1.8.11.custom.css' => 'screen');
        $this->template->scripts = array('assets/js/jquery-1.4.4.min.js','assets/js/jquery-ui-1.8.7.custom.min');

        $data['patient_list'] = $patient_model->searchPatientByKeyword($keyword);

        $this->template->content = View::factory("patient/search",$data);
    }

    public function action_save() {
        $consult = new Model_Consult();
        if(isset($_POST['method'])) {
            if($_POST['method']=="save") {
                print_r($_POST);
                $p_id = $_POST['p_id'];
                $c_id = $consult->getConsultID($p_id);
                $consult->updateStokeData($p_id, $c_id, $_POST);
                $consult->updateNIHSSData($p_id, $c_id, $_POST);
                $consult->updateLaboratoryData($p_id, $c_id, $_POST);
                $consult->updateStrokePhyData($p_id, $c_id, $_POST);
                $consult->updateEstimateData($p_id, $c_id, $_POST);
                $consult->updateManagementData($p_id, $c_id, $_POST);
                echo "Yes";
            }
        }else {
            echo "Fail";
        }
    }

    public function action_newpatient() {
        $patient_model = new Model_Patient();
        $result = $patient_model->addNewPatient($_POST);
        $p_id = $result[0];
        $this->request->redirect("../patient/profile/".$p_id);
    }
}
?>
